/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ResultEditor.SpanEdit;

import ResultEditor.Annotations.Depot;
import java.lang.annotation.Annotation;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;

/**
 *
 * @author leng 2010-6-14 2:23am
 */
public class SpanEditor {
    private final JTextComponent tc;

    public SpanEditor(JTextComponent tc){
        this.tc = tc;
    }

    final private int delete = 0, headextendtoLeft = 1, tailextendtoRight = 2,
            headShortentoRight = 3, tailShortentoLeft =4;
    
    public int editCurrentDisplayedSpan(int _type){
        ResultEditor.Annotations.Depot depot = new  ResultEditor.Annotations.Depot();
        if ( depot == null ) return 0;

        String span;
        int modifiedEntries = -1;
        // get latest accessed annotation in the query
        
        // get latest annotation
        ResultEditor.Annotations.Annotation annotation = ResultEditor.WorkSpace.WorkSet.currentAnnotation;

        int start = annotation.spanstart;
        int end = annotation.spanend;

        switch(_type){
            case headextendtoLeft:
                if (start == 0 ){
                    commons.Tools.beep();
                    return 0;
                }

                break;

            case tailextendtoRight:
                if (end >= tc.getDocument().getLength() ){
                    commons.Tools.beep();
                    return 0;
                }
                break;

            case headShortentoRight:
                if ( start == end - 1 ){
                    commons.Tools.beep();
                    return 0;
                }

                break;

            case tailShortentoLeft:
                if ( end == start + 1 ){
                    commons.Tools.beep();
                    return 0;
                }
                break;
        }

        // filename of current text source
        String filename = ResultEditor.WorkSpace.WorkSet.getCurrentFile().getName();

        // index: annotation index in vector of anntation of this article in depot
        int uniqueindex = ResultEditor.WorkSpace.WorkSet.currentAnnotation.uniqueIndex;

        switch(_type){
            case delete:

                modifiedEntries = depot.annotation_uid( filename, uniqueindex );
                Depot.SelectedAnnotationSet.removeAnnotationUniqueIndex(uniqueindex);
                break;
            case headextendtoLeft:
                span = getspan(start-1, end);
                modifiedEntries = depot.AnnotationRangeSet( filename, uniqueindex, span, start, end, headextendtoLeft);
                break;
            case tailextendtoRight:
                span = getspan(start, end+1);
                modifiedEntries = depot.AnnotationRangeSet( filename, uniqueindex, span, start, end, tailextendtoRight);
                break;
            case headShortentoRight:
                span = getspan(start+1, end);
                modifiedEntries = depot.AnnotationRangeSet( filename, uniqueindex, span, start, end, headShortentoRight);
                
                break;
            case tailShortentoLeft:
                span = getspan(start, end-1);
                modifiedEntries = depot.AnnotationRangeSet( filename, uniqueindex, span, start, end, tailShortentoLeft);
                break;

                

        }
        
        return modifiedEntries;
    }


    public int editCurrentDisplayedSpan(String documentfilename, int annotation_uid, int _type){
        ResultEditor.Annotations.Depot depot = new  ResultEditor.Annotations.Depot();
        if ( depot == null ) return 0;

        String span;
        int modifiedEntries = -1;
        // get latest accessed annotation in the query

        // get latest annotation
        ResultEditor.Annotations.Annotation annotation =
                depot.getAnnotationByUnique(documentfilename, annotation_uid);

        if (annotation == null){
            System.out.println("error 1012011558:: fail to find annotation!!");
            return 0;
        }

        int start = annotation.spanstart;
        int end = annotation.spanend;

        switch(_type){
            case headextendtoLeft:
                if (start == 0 ){
                    commons.Tools.beep();
                    return 0;
                }

                break;

            case tailextendtoRight:
                if (end >= tc.getDocument().getLength() ){
                    commons.Tools.beep();
                    return 0;
                }
                break;

            case headShortentoRight:
                if ( start == end - 1 ){
                    commons.Tools.beep();
                    return 0;
                }

                break;

            case tailShortentoLeft:
                if ( end == start + 1 ){
                    commons.Tools.beep();
                    return 0;
                }
                break;
        }

        // filename of current text source
        //String filename = ResultEditor.WorkSpace.WorkSet.getCurrentFile().getName();

        // index: annotation index in vector of anntation of this article in depot
        

        switch(_type){
            case delete:

                modifiedEntries = depot.annotation_uid( documentfilename, annotation_uid );
                Depot.SelectedAnnotationSet.removeAnnotationUniqueIndex(annotation_uid);
                break;
            case headextendtoLeft:
                span = getspan(start-1, end);
                modifiedEntries = depot.AnnotationRangeSet( documentfilename, annotation_uid, span, start, end, headextendtoLeft);
                break;
            case tailextendtoRight:
                span = getspan(start, end+1);
                modifiedEntries = depot.AnnotationRangeSet( documentfilename, annotation_uid, span, start, end, tailextendtoRight);
                break;
            case headShortentoRight:
                span = getspan(start+1, end);
                modifiedEntries = depot.AnnotationRangeSet( documentfilename, annotation_uid, span, start, end, headShortentoRight);

                break;
            case tailShortentoLeft:
                span = getspan(start, end-1);
                modifiedEntries = depot.AnnotationRangeSet( documentfilename, annotation_uid, span, start, end, tailShortentoLeft);
                break;



        }

        return modifiedEntries;
    }


    public String getspan(int start, int end){
        String spantext;
        try {
            int offset = ResultEditor.Conflicts.ConflictButtonPainter.getScreenOffset(start, 0);
            spantext = tc.getDocument().getText(start + offset, end - start);
            return spantext;
        } catch (BadLocationException ex) {
            Logs.ShowLogs.printErrorLog( ex.toString() );
            return null;
        }
    }

}
